//
//  UserAccount.swift
//  Weibo
//
//  Created by mac on 16/3/14.
//  Copyright © 2016年 hw.chow. All rights reserved.
//

import Foundation

class UserAccount:NSObject {
    //用于调用access_token，接口获取授权后的access token
    var access_token:String?
    //access_token的生命周期，单位是秒数 token过多少秒会过期
    var expires_in: NSTimeInterval = 0{
    
        didSet{
        expires_date = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    //token 过期的日期  "expires_date": 2021-03-14 02:49:02 +0000
    //开发者过期时间5年，普通用户过期时间是3天
    var expires_date: NSDate?
    
    //当前授权用户的UID
    var uid: String?
    //用户名称
    var name: String?
    //用户头像地址(大图),180x180像素
    var avatar_large: String?
    
    
    //KVC设置初始值
    init(dict: [String: AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    //过滤key
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    
    //重写对象的描述信息
    override var description: String {
            
            //使用KVC方式，获取对象的字典信息
            let keys = ["access_token","expires_in","uid","name","avatar_large","expires_date"]
            let dict = self.dictionaryWithValuesForKeys(keys)
            return dict.description
        
    }
    
    /*
    使用 归档和解归档  做数据的保存和获取
    */
    //保存自定义对象
    func saveAccount(){
        
        //保存到沙盒
        //拼接保存路径
        let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.plist")
        print(path)
        //将对象保存到沙盒路径中
        NSKeyedArchiver.archiveRootObject(self, toFile: path)
    }
    
    //读取磁盘中的保存的用户信息
    class func loadUserAccount() -> UserAccount? {
        let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.plist")
        
        if let account = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? UserAccount {
            //并且token没有过期，最后才会返回一个用户对象
            //NSDate() 获取当前日期
            if account.expires_date?.compare(NSDate()) == NSComparisonResult.OrderedDescending{
            return account
            }
        }
        return nil
    }
    //实现协议方法
    
    //解归档操作,必须实现的方法,将保存在磁盘中的二进制数据 --> 自定义对象
    //这个过程和反序列化类似
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        //如果是进本数据类型，可以直接解归档到基本数据类型
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        uid = aDecoder.decodeObjectForKey("uid") as? String
        name = aDecoder.decodeObjectForKey("name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        expires_date = aDecoder.decodeObjectForKey("exoires_date") as? NSDate
    }
    
    //归档操作,将自定义对象 --> 二进制数据 --> 保存在磁盘中
    //这个过程和序列化类似
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(name, forKey: "name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
        aCoder.encodeObject(expires_date, forKey: "expires_date")
    }

    
}
